
package com.kaciula.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;

public class StorageHelper {

    private static BroadcastReceiver mExternalStorageReceiver;

    private static boolean mExternalStorageAvailable = false;

    private static boolean mExternalStorageWriteable = false;

    public static void startWatchingExternalStorage(Context ctx) {
        mExternalStorageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                updateExternalStorageState();
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filter.addAction(Intent.ACTION_MEDIA_REMOVED);
        ctx.registerReceiver(mExternalStorageReceiver, filter);
        updateExternalStorageState();
    }

    public static void stopWatchingExternalStorage(Context ctx) {
        ctx.unregisterReceiver(mExternalStorageReceiver);
    }

    private static void updateExternalStorageState() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }
    }
    
    public static boolean isAvailable() {
        return mExternalStorageAvailable;
    }

    public static boolean isAvailableAndWritable() {
        return mExternalStorageAvailable && mExternalStorageWriteable;
    }

    public static Bitmap getBitmap(String path, int inSampleSize) {
        if (mExternalStorageAvailable) {
            String externalPath = Environment.getExternalStorageDirectory().getName();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = inSampleSize;

            return BitmapFactory.decodeFile(externalPath + "/" + path, options);
        } 

        return null;
    }
    
    public static File getFile(String path) {
        if (mExternalStorageAvailable && mExternalStorageWriteable) {
            String externalPath = Environment.getExternalStorageDirectory().getName();
            File file = new File(externalPath + "/" + path);
            return file;
        }
        
        return null;
    }

    public static boolean deleteFile(String path) {
        // remove pictures of the waste from external storage
        if (mExternalStorageAvailable && mExternalStorageWriteable) {
            String externalPath = Environment.getExternalStorageDirectory().getName();
            File file = new File(externalPath + "/" + path);

            if (file.exists() && file.isFile()) {
                file.delete();
            }

            return true;
        }

        return false;
    }

    public static boolean createDir(String path) {
        if (mExternalStorageAvailable && mExternalStorageWriteable) {
            String externalPath = Environment.getExternalStorageDirectory().getName();

            File file = new File(externalPath + "/" + path);

            if (!file.exists() || !file.isDirectory())
                file.mkdirs();

            return true;
        }

        return false;
    }
}
